package com.example.myapplication.tack_29.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.tack_29.data.ProfessionData;
import com.example.myapplication.tack_29.data.QueryData;
import com.example.myapplication.utils.OkHttpTool;
import com.example.myapplication.utils.StateData;
import com.google.android.material.tabs.TabLayout;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class PersonFragment extends Fragment {
    private List<String> listManage;
    private View view;
    private Handler handler;
    private TabLayout manage;
    private List<ProfessionData.RowsDTO> listProfession;
    private EditText mostEducation;
    private EditText education;
    private EditText address;
    private EditText experience;
    private EditText individualResume;
    private EditText money;
    private JSONObject jsonObject;
    private JSONObject jsonObject2;
    private TextView submit;
    private String positionId_;
    private String positionId_02;
    private Map<String,String> map;
    private TextView mostEducation1;
    private TextView education1;
    private TextView address1;
    private TextView experience1;
    private TextView individualResume1;
    private TextView money1;
    private TextView positionId1;
    private LinearLayout line1;
    private LinearLayout line2;
    private LinearLayout line3;
    private TextView mostEducation2;
    private TextView education2;
    private TextView address2;
    private TextView experience2;
    private TextView individualResume2;
    private TextView money2;
    private TextView edit;
    private void init() {
        listManage = new ArrayList<>();
        listManage.add("新增简历");
        listManage.add("查询简历");
        listManage.add("编辑简历");
        handler = new Handler();
        manage = view.findViewById(R.id.manage);
        mostEducation = view.findViewById(R.id.mostEducation);
        education = view.findViewById(R.id.education);
        address = view.findViewById(R.id.address);
        experience = view.findViewById(R.id.experience);
        individualResume = view.findViewById(R.id.individualResume);
        money = view.findViewById(R.id.money);
        jsonObject = new JSONObject();
        jsonObject2 = new JSONObject();
        submit = view.findViewById(R.id.submit);
        map = new HashMap<>();
        mostEducation1 = view.findViewById(R.id.mostEducation1);
        education1 = view.findViewById(R.id.education1);
        address1 = view.findViewById(R.id.address1);
        experience1 = view.findViewById(R.id.experience1);
        individualResume1 = view.findViewById(R.id.individualResume1);
        money1 = view.findViewById(R.id.money1);
        positionId1 = view.findViewById(R.id.positionId1);
        line1 = view.findViewById(R.id.line1);
        line2 = view.findViewById(R.id.line2);
        line3 = view.findViewById(R.id.line3);
        line1.setVisibility(View.VISIBLE);
        line2.setVisibility(View.GONE);
        line3.setVisibility(View.GONE);
        mostEducation2 = view.findViewById(R.id.mostEducation2);
        education2 = view.findViewById(R.id.education2);
        address2 = view.findViewById(R.id.address2);
        experience2 = view.findViewById(R.id.experience2);
        individualResume2 = view.findViewById(R.id.individualResume2);
        money2 = view.findViewById(R.id.money2);
        edit = view.findViewById(R.id.edit);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.tack_29_fragment_person2, container, false);
        init();
        getManage();
        getProfession();

        initEdit();
        return view;
    }
    private void initEdit() {
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s1 = mostEducation2.getText().toString();
                String s2 = education2.getText().toString();
                String s3 = address2.getText().toString();
                String s4 = experience2.getText().toString();
                String s5 = individualResume2.getText().toString();
                String s6 = money2.getText().toString();
                String s7 = map.get(positionId_02);
                if(!"".equals(s1) && !TextUtils.isEmpty(s1) && !"".equals(s2) && !TextUtils.isEmpty(s2) &&
                        !"".equals(s3) && !TextUtils.isEmpty(s3) &&
                        !"".equals(s4) && !TextUtils.isEmpty(s4) &&
                        !"".equals(s5) && !TextUtils.isEmpty(s5) &&
                        !"".equals(s6) && !TextUtils.isEmpty(s6) &&
                        !"".equals(s7) && !TextUtils.isEmpty(s7) ){
                    try {
                        jsonObject2.put("id",String.valueOf(2));
                        jsonObject2.put("mostEducation",s1);
                        jsonObject2.put("education",s2);
                        jsonObject2.put("address",s3);
                        jsonObject2.put("experience",s4);
                        jsonObject2.put("individualResume",s5);
                        jsonObject2.put("money",s6);
                        jsonObject2.put("positionId",s7);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    String json = jsonObject2.toString();
                    setEdit(json);
                }else{
                    Toast.makeText(PersonFragment.this.getContext(),"信息不能为空",Toast.LENGTH_SHORT).show();
                }


            }
        });
    }
    private void setEdit(String json) {
        String url ="prod-api/api/job/resume";
        OkHttpTool.doNetRequest(url, OkHttpTool.PUT, StateData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                StateData stateData = (StateData)obj;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if(stateData.getCode() == 200){
                            Toast.makeText(PersonFragment.this.getContext(),stateData.getMsg(),Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(PersonFragment.this.getContext(),stateData.getMsg(),Toast.LENGTH_SHORT).show();

                        }
                    }
                });

            }

            @Override
            public void onFailure(String msg) {

            }
        },json);
    }


    //查询
    private void getQuery() {
        String url = "prod-api/api/job/resume/queryResumeByUserId/2";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, QueryData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                QueryData queryData = (QueryData)obj;
                if(queryData.getCode() == 200){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            if(!TextUtils.isEmpty(queryData.getData().getMostEducation())){
                                mostEducation1.setText(queryData.getData().getMostEducation());
                            }else{
                                mostEducation1.setText("暂无信息");
                            }
                            if(!TextUtils.isEmpty(queryData.getData().getEducation())){
                                education1.setText(queryData.getData().getEducation());
                            }else{
                                education1.setText("暂无信息");
                            }
                            if(!TextUtils.isEmpty(queryData.getData().getAddress())){
                                address1.setText(queryData.getData().getAddress());
                            }else{
                                address1.setText("暂无信息");
                            }
                            if(!TextUtils.isEmpty(queryData.getData().getExperience())){
                                experience1.setText(queryData.getData().getExperience());
                            }else{
                                experience1.setText("暂无信息");
                            }
                            if(!TextUtils.isEmpty(queryData.getData().getIndividualResume())){
                                individualResume1.setText(queryData.getData().getIndividualResume());
                            }else{
                                individualResume1.setText("暂无信息");
                            }
                            if(!TextUtils.isEmpty(queryData.getData().getMoney())){
                                money1.setText(queryData.getData().getMoney());
                            }else{
                                money1.setText("暂无信息");
                            }
                            if(!TextUtils.isEmpty(String.valueOf(queryData.getData().getPositionId()))){
                                String s = map.get(queryData.getData().getPositionId());
                                positionId1.setText(s);
                            }else{
                                positionId1.setText("暂无信息");
                            }


                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void initSubmit() {
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s1 = mostEducation.getText().toString();
                String s2 = education.getText().toString();
                String s3 = address.getText().toString();
                String s4 = experience.getText().toString();
                String s5 = individualResume.getText().toString();
                String s6 = money.getText().toString();
                String s7 = map.get(positionId_);
                if(!"".equals(s1) && !TextUtils.isEmpty(s1) && !"".equals(s2) && !TextUtils.isEmpty(s2) &&
                        !"".equals(s3) && !TextUtils.isEmpty(s3) &&
                        !"".equals(s4) && !TextUtils.isEmpty(s4) &&
                        !"".equals(s5) && !TextUtils.isEmpty(s5) &&
                        !"".equals(s6) && !TextUtils.isEmpty(s6) &&
                        !"".equals(s7) && !TextUtils.isEmpty(s7) ){
                    try {
                        jsonObject.put("mostEducation",s1);
                        jsonObject.put("education",s2);
                        jsonObject.put("address",s3);
                        jsonObject.put("experience",s4);
                        jsonObject.put("individualResume",s5);
                        jsonObject.put("money",s6);
                        jsonObject.put("positionId",s7);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    String json = jsonObject.toString();
                    getResume(json);
                }else{
                    Toast.makeText(PersonFragment.this.getContext(),"信息不能为空",Toast.LENGTH_SHORT).show();
                }


            }
        });
    }

    private void getResume(String json) {
        String url = "prod-api/api/job/resume";
        OkHttpTool.doNetRequest(url, OkHttpTool.POST, StateData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                StateData stateData = (StateData)obj;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if(stateData.getCode() == 200){
                            Toast.makeText(PersonFragment.this.getContext(),stateData.getMsg(),Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(PersonFragment.this.getContext(),stateData.getMsg(),Toast.LENGTH_SHORT).show();

                        }
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        },json);
    }
    //新建简历
    private void getProfession() {
        String url = "prod-api/api/job/profession/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, ProfessionData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                ProfessionData profession = (ProfessionData)obj;
                if(profession.getCode() == 200){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            listProfession = profession.getRows();
                            initSpinner();
                            initSpinner2();
                        }
                    });

                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }
    private void initSpinner2() {
        List<String> list = new ArrayList();
        for (ProfessionData.RowsDTO rowsDTO : listProfession) {
            map.put(rowsDTO.getProfessionName(),String.valueOf(rowsDTO.getId()));
        }
        for (int i = 0; i < listProfession.size(); i++) {
            list.add(listProfession.get(i).getProfessionName());
        }

        ArrayAdapter<String> starAdapter  = new ArrayAdapter(PersonFragment.this.getContext(),R.layout.item_select,list);
        starAdapter .setDropDownViewResource(R.layout.tack_25_item_dropdown);
        Spinner positionId = view.findViewById(R.id.positionId2);
        positionId.setAdapter(starAdapter );
        positionId.setSelection(0);
        positionId.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                positionId_02 = list.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void initSpinner() {
        List<String> list = new ArrayList();
        for (ProfessionData.RowsDTO rowsDTO : listProfession) {
            map.put(rowsDTO.getProfessionName(),String.valueOf(rowsDTO.getId()));
        }
        for (int i = 0; i < listProfession.size(); i++) {
            list.add(listProfession.get(i).getProfessionName());
        }

        ArrayAdapter<String> starAdapter  = new ArrayAdapter(PersonFragment.this.getContext(),R.layout.item_select,list);
        starAdapter .setDropDownViewResource(R.layout.tack_25_item_dropdown);
        Spinner positionId = view.findViewById(R.id.positionId);
        positionId.setAdapter(starAdapter );
        positionId.setSelection(0);
        positionId.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                positionId_ = list.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void getManage() {
        for (String s : listManage) {
            TabLayout.Tab tab =manage.newTab();
            tab.setText(s);
            manage.addTab(tab);
        }
        ManageAdapter();
    }

    private void ManageAdapter() {
        manage.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                if("新增简历".equals(listManage.get(position))){
                    line1.setVisibility(View.VISIBLE);
                    line2.setVisibility(View.GONE);
                    line3.setVisibility(View.GONE);
                    getProfession();
                }
                if("查询简历".equals(listManage.get(position))){
                    line2.setVisibility(View.VISIBLE);
                    line1.setVisibility(View.GONE);
                    line3.setVisibility(View.GONE);
                    getQuery();
                }
                if("编辑简历".equals(listManage.get(position))){
                    line3.setVisibility(View.VISIBLE);
                    line1.setVisibility(View.GONE);
                    line2.setVisibility(View.GONE);

                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }


}